﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ByTagCondition.cs" company="Microsoft">
//   Microsoft
// </copyright>
// <summary>
//   Defines the ByTagCondition type.
// </summary>
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// --------------------------------------------------------------------------------------------------------------------

namespace MySemanticSearch.Web.QueryBuilder.Conditions
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;

    /// <summary>
    /// This condition handles how to filter documents by tags
    /// </summary>
    public class ByTagCondition : ICondition
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ByTagCondition"/> class.
        /// </summary>
        /// <param name="fileTable">
        /// The file table.
        /// </param>
        /// <param name="tags">
        /// The tags to filter
        /// </param>
        public ByTagCondition(string fileTable, LinkedList<string> tags)
        {
            this.FileTable = fileTable;
            this.Tags = tags;
        }

        /// <summary>
        /// Gets or sets FileTable.
        /// </summary>
        private string FileTable { get; set; }

        /// <summary>
        /// Gets or sets Tags.
        /// </summary>
        private LinkedList<string> Tags { get; set; }
        
        /// <summary>
        /// Gets the condition in the form of a string
        /// </summary>
        /// <returns>
        /// The condition to filter by tags
        /// </returns>
        public string GetCondition()
        {
            string tags = this.Tags.Aggregate(string.Empty, (current, tag) => current + ("'" + tag + "',"));

            tags = tags.TrimEnd(',');

            return String.Format(
                CultureInfo.InvariantCulture, App_Data.SqlConditions.ByTagCondition, this.FileTable, tags);
        }
    }
}